package com.xjscrm.console.chat.core.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjscrm.common.entity.WwGroup;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.recv.RPullGroupMsg;
import com.xjscrm.console.chat.frame.send.SPullGroupResultMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.dto.chat.ChatGroupDTO;
import com.xjscrm.console.service.customer.chat.ChatTalkRecordService;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionStaffRelService;
import com.xjscrm.console.service.customer.group.WwGroupService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/12 09:41
 */
@Component
@Slf4j
public class ChatPullGroupMsgHandler extends ChatBaseMsgHandler<RPullGroupMsg> {

    @Autowired
    private ChatTalkSessionService chatTalkSessionService;
    @Autowired
    private ChatTalkSessionStaffRelService chatTalkSessionStaffRelService;
    @Autowired
    private ChatTalkRecordService chatTalkRecordService;

    @Autowired
    private WwGroupService groupService;

    @Override
    public SResultMsg doHandle(RPullGroupMsg content, ChannelHandlerContext ctx) {

        SPullGroupResultMsg pullGroupResultMsg = new SPullGroupResultMsg();

        Page<ChatGroupDTO> result = new Page<>(content.getSize(), content.getCurrent());
        pullGroupResultMsg.setPage(result);

        try {
            Long merId = content.getMerId();

            Page<WwGroup> customerList = groupService.pageFromChat(merId, content);
            if (ObjectUtils.isEmpty(customerList.getRecords())) {
                return pullGroupResultMsg;
            }
            List<ChatGroupDTO> resultRecords = new ArrayList<>();
            for (WwGroup record : customerList.getRecords()) {
                ChatGroupDTO chatCustomerDTO = new ChatGroupDTO();

//                chatCustomerDTO.setGroupAvatar(record.geth());
                chatCustomerDTO.setGroupName(record.getGroupName());
                chatCustomerDTO.setGroupWxId(record.getGroupWxid());

                resultRecords.add(chatCustomerDTO);
            }
            result.setRecords(resultRecords);

            result.setRecords(resultRecords);
        } catch (Exception e) {
            log.error("拉取机器人群聊失败", e);
            throw new BusinessException("拉取机器人群聊失败");
        }
        return pullGroupResultMsg;
    }

    @Override
    public boolean checkMsg(RPullGroupMsg baseMsg) {
        return super.checkMsg(baseMsg) || !Objects.nonNull(baseMsg.getDeviceUniqueId());
    }

    @Override
    public Class<RPullGroupMsg> msgClass() {
        return RPullGroupMsg.class;
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_pull_group;
    }
}
